import { observable, action, toJS } from 'mobx';
import { ComsStore } from './tables/common';
import * as API_MONITOR from '../apis/monitor';
import { WeaLocaleProvider } from 'ecCom';
import { remove, isEmpty } from 'lodash';

const getLabel = WeaLocaleProvider.getLabel;

export class DocMonitorStore extends ComsStore {
  @observable title = getLabel(16757, '文档监控');
  @observable loading = false;
  @observable selectedTreeKeys = [];
  @observable expandedKey = [];
  @observable secCategory = '';
  @observable secCategoryName = '';
  @observable operateType = [];
  @observable showSearchAd = false;
  @observable showBtn = {}; // 操作按钮的状态
  @observable tabData = [];
  @observable showBtnKey = '';
  @observable quickSearchValue = '';
  @observable isNormalToActive = true;
  @observable isQueueLeaving = false;
  @observable subCompanyId = '0';
  @observable hasRight = false;
  @observable rightStr = '';
  @action

  setTitle = (title) => {
    this.title = title;
  }
  setLoading = (title) => {
    this.title = title;
  }
  setShowSearchAd = (showSearchAd) => {
    this.showSearchAd = showSearchAd;
  }

  clearSelectedTreeKeys = () => {
    this.expandedKey = [];
    // this.selectedTreeKeys = [];
    this.tabData = [];
    this.form.reset();
    this.quickSearchValue = '';
    this.comsWeaTableStore.setSelectedRowKeys([]);
  }
  clearSearchValue = () => {
    this.quickSearchValue = '';
    this.form.reset();
  }
  setSelectedTreeKeys = (key, shouldExpandedKey) => {
    this.selectedTreeKeys = [key];
    if (key.length !== 0) {
      this.secCategory = this.getKey();
      this.doSearch({ secCategory: this.secCategory });
      this.getRightMenu('Monitor', { secCategory: this.secCategory });
    } else {
      this.secCategory = '';
    }
    this.setShowBtn(this.operateType[0].key);
    this.form.reset();
    this.form.updateFields({
      seccategory: {
        value: this.secCategory,
        valueSpan: this.secCategoryName,
        valueObj: [
          {
            id: this.secCategory,
            name: this.secCategoryName,
          },
        ],
      },
    });
    this.quickSearchValue = '';
    this.tabData = [];
    this.setExpandedKeysByChooseId(key);
    this.comsWeaTableStore.setSelectedRowKeys([]);
  }

  // 选中id 反向确定展开的id 且刷新右侧标题导航
  setExpandedKeysByChooseId = (id) => {
    const expandedKeys = toJS(this.expandedKey),
_treeDatas = toJS(this.treeDatas);
    if (!_treeDatas.find(o => o.domid === id)) {
       let tempExpandedKey = [];
       _treeDatas.forEach((item) => {
         if (!isEmpty(item.childs)) {
           const tempkey = this.getItem(item, id);
           tempkey.forEach((i) => {
             if (i.getNode) {
               const temptreeData = this.getShowItem(tempkey, i.item.domid, i.item.pid);
               this.setTabData(temptreeData);
               temptreeData.forEach(o => tempExpandedKey.push(o.domid));
               remove(tempExpandedKey, o => toJS(expandedKeys).indexOf(o) > -1 || o === id);
               tempExpandedKey = tempExpandedKey.concat(toJS(expandedKeys));
               this.expandedKey = tempExpandedKey;
             }
           });
         }
       });
    } else {
      _treeDatas.forEach((item) => {
        if (item.domid === id) this.setTabData([item]);
      });
    }
  }

  setSecCategory = (key) => {
    this.secCategory = key;
  }
  setTabData = (tabData) => {
    this.tabData = tabData;
  }
  setShowBtn = (key) => {
    let temp = this.showBtn;
    for (let i in temp) {
      if (i === key) {
        temp[i] = 1;
      } else {
        temp[i] = 0;
      }
    }
    this.showBtn = temp;
    this.showBtnKey = key;
  }
  setExpandedKey = (expandedKey) => {
    this.expandedKey = expandedKey;
  }
  setQuickSearchValue = (quickSearchValue) => {
    this.quickSearchValue = quickSearchValue;
  }
  setIsNormalToActive = (isNormalToActive) => {
    this.isNormalToActive = isNormalToActive;
  }
  setIsQueueLeaving = (isQueueLeaving) => {
    this.isQueueLeaving = isQueueLeaving;
  }
  setSubCompanyId = (subCompanyId) => {
    this.subCompanyId = subCompanyId;
  }

  getOperateType = (params) => {
    API_MONITOR.getOperateType(params).then((data) => {
      data.operates.forEach((item, index) => {
        const key = item.key;
        if (index === 0) {
          this.showBtn[key] = 1;
          this.showBtnKey = key;
        } else {
 this.showBtn[key] = 0; 
}
      });
      this.operateType = data.operates;
    });
  }
  doOperate = (params) => {
    API_MONITOR.doOperate(params).then((data) => {
      // this.doSearch({ secCategory: this.secCategory, operation: this.showBtnKey });
      this.doSearch({ operation: this.showBtnKey, ...this.form.getFormParams(),secCategory: this.secCategory });
    });
  }
  doSearch = (params) => {
    this.loading = true;
    API_MONITOR.doSearch(params).then((data) => {
      this.comsWeaTableStore.getDatas(data.sessionkey, 1);
      this.comsWeaTableStore.setSelectedRowKeys([]);
      this.dataKey = data.sessionkey;
      this.loading = false;
    });
  }
  getSeparatRight = (params) => {
    API_MONITOR.getSeparatRight(params).then((data) => {
      this.rightStr = data.rightStr;
      if (data.status === '1') {
        this.hasRight = true;
        this.getTreeDatas('Monitor', { rightStr: data.rightStr });
      } else {
        this.hasRight = false;
      }
    });
  }
  getItem = (item, selected) => {
    let getNode = false;
    let returnItem = [];
    if (item.domid === selected) {
      getNode = true;
      returnItem.push({ item, getNode });
    } else {
      returnItem.push({ item, getNode });
      if (item.haschild) {
        item.childs.forEach((i) => {
          let tempResult = [];
          tempResult = this.getItem(i, selected);
          returnItem = returnItem.concat(tempResult);
        });
      }
    }
    return returnItem;
  }
  getShowItem = (tempResult, domid, pid) => {
    let result = [];
    let temp = [];
    tempResult.forEach((item) => {
      if (item.item.domid === domid) {
        result.unshift(item.item);
      }
    });
    tempResult.forEach((item) => {
      if (item.item.key === pid) {
        result.unshift(item.item);
        temp = [];
        if (pid !== '') {
          temp = this.getShowItem(tempResult, item.item.domid, item.item.pid);
          temp.pop();
          result = temp.concat(result);
        }
      }
    });
    return result;
  }
  getKey = () => {
    let keySearch = '';
    if (this.selectedTreeKeys.length === 0) return '';
    const key = this.selectedTreeKeys[0];
    this.treeDatas.forEach((item) => {
      if (item.domid === key) {
        keySearch = item.key;
        this.secCategoryName = item.name;
      } else if (item.haschild) {
          const tempkey = this.getItem(item, key);
          tempkey.forEach((i) => {
            if (i.getNode) {
              keySearch = i.item.key;
              this.secCategoryName = i.item.name;
            }
          });
        }
    });
    return keySearch;
  }
}
